<?php

// Init owncloud

require_once(OC_App::getAppPath('resumes') . "/lib/helper.php");

OCP\JSON::callCheck();
$companyid = 0; 
if(!OC_User::isLoggedIn()) {
   $app = PFactory::getApplication(); 
   $companyid = $app->getUserState('resumeshare_companyid', 0); 
   if(!$companyid) {
      echo '您需要先登录'; 
      exit;
   }
}
else {
   $companyid = PFactory::getMasterUser()->id;
}

$resumes = PResumeController::getResumeAsFiles($companyid);
if(empty($resumes)) {
   echo '简历未找到';
   exit;
}

$source = JRequest::getString('source', '51job');
// only allow view one resume at a time      
$resume = $resumes[0];

// security check: the request must be valid part of resume->source 
if(strpos($resume->source, $source) === FALSE) {
   // invalid source 
   echo '简历未找到';
   exit;
}

// update new status 
if($resume->is_new) PResumeController::setResumeNewStatus($resume->id); 

// $resumefile is an array 
$resumefiles = PStorage::getResumeFilePath($companyid, $resume, $source);

if(empty($resumefiles)) {
   echo '简历未找到';
   exit; 
}

// a path is:  aa/bb/cc/<hash key>/<version>/<source>/resume.html. For a upload file, it has company id to deferentiate each upload 
$resumefile = array_shift($resumefiles); 
$content = file_get_contents($resumefile);
$content = preg_replace("/__ppp_match_result__/", $resume->match_degree.'%', $content); 
$content = preg_replace("/__ppp_display_none__/", "none", $content); 
echo $content; 
